package com.qingfeng.client;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import com.qingfeng.client.service.WxPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付配置文件
 */
@Component
public class WxConfig implements WXPayConfig {


    public static String BASEURL;

    public static  String APPID;//应用id

    public static String APPSECRET;//app密钥

    public static  String SECRET;//支付秘钥

    public static  String MCHID;//微信支付商户号

    public static  String NOTIFYURL;//通知接口
    private byte[] certData;
    private static WxConfig wxConfig;

    private static String apiclientCertPath;

    private static WXPay wxPay;

    @Value("${wx.appid}")
    public  void setAPPID(String APPID) {
        WxConfig.APPID = APPID;
    }
    @Value("${apiclientCertPath}")
    public void setApiclientCertPath(String apiclientCertPath) {
        this.apiclientCertPath = apiclientCertPath;
    }
    @Value("${baseUrl}")
    public  void setBASEURL(String BASEURL) {
        WxConfig.BASEURL = BASEURL;
    }
    @Value("${wx.mchsecret}")
    public  void setSECRET(String SECRET) {
        WxConfig.SECRET = SECRET;
    }
    @Value("${wx.mchid}")
    public  void setMCHID(String MCHID) {
        WxConfig.MCHID = MCHID;
    }
    @Value("${wx.notifyUrl}")
    public  void setNOTIFYURL(String NOTIFYURL) {
        WxConfig.NOTIFYURL = NOTIFYURL;
    }

    @Value("${wx.secret}")
    public  void setAPPSECRET(String APPSECRET) {
        WxConfig.APPSECRET = APPSECRET;
    }

    public  void setWxConfig(WxConfig wxConfig) {
        WxConfig.wxConfig = wxConfig;
    }

    public  void setWxPay(WXPay wxPay) {
        WxConfig.wxPay = wxPay;
    }

    private void setCert() throws IOException {
        System.out.println("apiclientCertPath:"+apiclientCertPath);
        File file = new File(apiclientCertPath);
        InputStream inputStream = new FileInputStream(file);
        certData = new byte[(int) file.length()];
        inputStream.read(certData);
        inputStream.close();
    }

    public static WxConfig getPayInstance(){
        if (wxConfig==null){
            synchronized (WxConfig.class){
                if (wxConfig==null){
                    wxConfig = new WxConfig();
                    try {
                        wxConfig.setCert();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return wxConfig;
    }

    public static WXPay getWxPay(){
        if (wxPay==null){
            synchronized (WxConfig.class){
                if (wxConfig==null){
                    wxConfig = new WxConfig();
                    try {
                        wxConfig.setCert();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            wxPay = new WXPay(wxConfig);
        }
        return wxPay;
    }

    @Override
    public String getAppID() {
        return APPID;
    }

    @Override
    public String getMchID() {
        return MCHID;
    }

    @Override
    public String getKey() {
        return SECRET;
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}
